﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.Utils.PdfReportGenerator;
using VietThuongLMS.Services;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Areas.Admin.Models;
using DCS.ComLib.Config;
using VietThuongLMS.Utils;
namespace VietThuongLMS.Areas.Admin.Controllers
{
    public class BaoCaoThongKeController : PdfViewController
    {
        //
        // GET: /Admin/BaoCaoThongKe/

        [Authorize(Roles = "Admin")]
        public ActionResult ThongKeNhanh()
        {
            return View();
        }
        //public ActionResult DownloadPDF(int option)
        //{
        //    //var hvl = HocVien_LopService.GetHocVien_LopByMaTaiKhoan(16);
        //    //return this.ViewPdf("testing", "TestingReport", hvl);
        //    var hvvm = new HocVienViewModel();
        //    if (option==1)
        //        hvvm = HocVienService.DanhSachHocVienMoi(10, 1);
        //    if (option==2)
        //        hvvm = HocVienService.DanhSachHocVienNghiHoanToan(10, 1);
        //    return this.ViewPdf();
        //}

        [Authorize(Roles = "Admin")]
        public ActionResult XuatBangLuong(int id, int m, int y)
        {
            var source = new PhanCongService().TatCaPhanCongTheoThang(id, m, y);
            if (source.ListMoRong.Count == 0)
                return this.Content("Giáo viên này không dạy lớp nào trong tháng " + m.ToString() + "/" + y.ToString());
            return this.ViewPdfXuatBangLuong(m, y, source);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult DanhSachHocVienMoi(int m, int y)
        {
            int page = 1;
            if (Request["p"]!=null)
            {
                page = int.Parse(Request["p"]);
            }
            var hvvm = HocVienService.DanhSachHocVienMoi(m,y,10,page );
            ViewData["pageLinks"] = Utils.GeneralHTMLString.HTMLForJavaScriptPagination(hvvm.TotalMoRong, 10, page, "RequestDanhSachHocVienMoi");
            //ViewData["optionToExportPDFfile"] = 1;
            ViewData["tieude"] = "Danh sách học viên mới tháng " + m.ToString() + "/" + y.ToString();
            return View("pvDanhSachHocVien",hvvm);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult DanhSachHocVienNghiHoanToan(int m, int y)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"]);
            }
            var hvvm = HocVienService.DanhSachHocVienNghiHoanToan(m,y,10,page);
            ViewData["pageLinks"] = Utils.GeneralHTMLString.HTMLForJavaScriptPagination(hvvm.TotalMoRong, 10, page, "RequestDanhSachHocVienNghiHoanToan");
            //ViewData["optionToExportPDFfile"] = 1;
            ViewData["tieude"] = "Danh sách dự đoán học viên nghỉ hoàn toàn tính đến tháng " + m.ToString() + "/" + y.ToString();
            return View("pvDanhSachHocVien", hvvm);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult BaoCaoDoanhThu()
        {
            List<ChiNhanh> items = ChiNhanhService.GetChiNhanhList();
            items.Add(new ChiNhanh() {ID=-1, TenChiNhanh="Tất cả"});
            ViewData["ChiNhanh"] = new SelectList(items, "ID", "TenChiNhanh");
            return View("BaoCaoDoanhThu");
        }

        [Authorize(Roles = "Admin")]
        public ActionResult RequestBaoCaoDoanhThu(int m, int y, int chinhanh)
        {
            BaoCaoTatCaViewModel bcvmtc = new BaoCaoTatCaViewModel();
            if (chinhanh != -1)
            {
                BaoCaoViewModel bcvm = new PhanCongService().BaoCaoDoanhThu(m, y, chinhanh);
                bcvmtc.ThemBaoCao(bcvm);
            }
            else
            {
                bcvmtc = new PhanCongService().BaoCaoDoanhThuTong(m, y);
            }
            bcvmtc.TinhTongChi(); bcvmtc.TinhTongThu(); bcvmtc.TinhTongLoiNhuan();
            ViewData["thoigian"] = m.ToString() + "/" + y.ToString();
            return PartialView("pvBaoCaoDoanhThu", bcvmtc);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult PhieuThuChi()
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            DoanhThuViewModel accountList = DoanhThuService.GetDoanhThuList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "BaoCaoThongKe", "PhieuThuChi", "?p=", accountList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(accountList);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            using (LMSDataEntities context = new LMSDataEntities())
            {
                ViewData["CBChiNhanh"] = new SelectList(context.ChiNhanhs.ToList(), "ID", "TenChiNhanh");
                return View();
            }
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Create(DoanhThuModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DoanhThu data = new DoanhThu();
                    data.MaChiNhanh = model.MaChiNhanh;
                    data.Ngay = model.Ngay;
                    data.TienThu = model.TienThu;
                    data.TienChi = model.TienChi;
                    data.LyDo = model.LyDo;
                    bool result = DoanhThuService.InsertDoanhThu(data);
                    if (result)
                        return RedirectToAction("PhieuThuChi", "BaoCaoThongKe", new { area = "Admin" });
                    using (LMSDataEntities context = new LMSDataEntities())
                    {
                        ViewData["CBChiNhanh"] = new SelectList(context.ChiNhanhs.ToList(), "ID", "TenChiNhanh");
                        return View(model);
                    }
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            DoanhThu account = DoanhThuService.GetDoanhThuById(id);
            using (LMSDataEntities context = new LMSDataEntities())
            {
                ViewData["CBChiNhanh"] = new SelectList(context.ChiNhanhs.ToList(), "ID", "TenChiNhanh");
                return View(account);
            }
        }

        [Authorize(Roles = "Admin")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(DoanhThu comp)
        {
            DoanhThuService.UpdateDoanhThu(comp);
            return RedirectToAction("PhieuThuChi", "BaoCaoThongKe", new { area = "Admin" });
        }

    }
}
